﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using snat.proxy.items;

namespace snat.proxy.sqlapi {
    public static class ConnectionQuery {

     /*   public static ListProxy<snat.proxy.items.Connection> All() {
            return Query.SQLQuery<snat.proxy.items.Connection>(
                String.Join(" ", Query.SELECT, Query.ALL, Query.FROM, "Connected", Query.NATURAL_JOIN, 
                                "(", Query.SELECT, Query.EntityID, Query.AS, Query.FromEntityID, ",", Query.EntityName, Query.AS, Query.Sender, 
                                     Query.FROM, Query.Entity, ")", Query.AS, "dummy1", Query.NATURAL_JOIN, 
                                "(", Query.SELECT, Query.EntityID, Query.AS, Query.ToEntityID, ",", Query.EntityName, Query.AS, Query.Recipient,
                                     Query.FROM, Query.Entity, ")", Query.AS, "dummy2"));
        }
        */

        


        public static ListProxy<Connection> Init() {
            return Query.SQLQuery<snat.proxy.items.Connection>(@"SELECT * FROM Connected WHERE Initial = 1 ",
@"SELECT * FROM Entity NATURAL JOIN (SELECT FromEntityID as EntityID FROM (select * from Connected WHERE Initial=1 LIMIT ?position,?bufferSize) as du UNION SELECT ToEntityID as EntityID FROM (select * from Connected WHERE Initial=1 LIMIT ?position,?bufferSize) as da ) as d1;");
        }


        public static ListProxy<Connection> All() {
            return Query.SQLQuery<snat.proxy.items.Connection>(@"SELECT * FROM Connected",
@"SELECT * FROM Entity NATURAL JOIN (SELECT FromEntityID as EntityID FROM (select * from Connected LIMIT ?position,?bufferSize) as du UNION SELECT ToEntityID as EntityID FROM (select * from Connected LIMIT ?position,?bufferSize) as da ) as d1;");
        }


        /*public static ListProxy<snat.proxy.items.Connection> ByEntityID(int id) {
            return Query.SQLQuery<snat.proxy.items.Connection>(@"SELECT * FROM Connected 
                                                       WHERE ToEntityID = " +id+" OR FromEntityID = "+id);
        }*/

        /*
        public static ListProxy<snat.proxy.items.Connection> Between(int fromID, int toID, bool directed = true) {
            if (directed) {
                return Query.SQLQuery<snat.proxy.items.Connection>(@"SELECT * FROM Connected
                                                          WHERE ToEntityID = " + toID + " AND FromEntityID = " + fromID);
            } else {
                return Query.SQLQuery<snat.proxy.items.Connection>(@"SELECT * FROM Connected
                                                      WHERE (ToEntityID = " + toID + " AND FromEntityID = " + fromID + @")
                                                      OR (ToEntityID = " + fromID + " AND FromEntityID = " + toID + ")");
            }
        }*/

    }
}
